/*
 * Filename: g:\espidf_workspace\idf_S3_T-Watch\main\network\http.cc
 * Path: g:\espidf_workspace\idf_S3_T-Watch\main\network
 * Created Date: Friday, June 20th 2025, 11:25:10 am
 * Author: sss
 *
 * Copyright (c) 2025 Your Company
 */
#include "http.h"
#include "esp_log.h"
#include "esp_err.h"
#include "esp_http_client.h"

static const char *TAG = "HTTP_CLIENT";

HTTPClient::HTTPClient()
{
}

HTTPClient::~HTTPClient()
{
}

void HTTPClient::get(const char *url, http_event_handle_cb _handle_cb)
{
    esp_http_client_config_t config = {
        .url = url,
        .event_handler = _handle_cb,
    };
    client = esp_http_client_init(&config);
    // 执行请求
    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK)
    {
        ESP_LOGI(TAG, "HTTP Status = %d", esp_http_client_get_status_code(client));
    }
}

void HTTPClient::post(const char *url, const char *post_data, http_event_handle_cb _handle_cb)
{
    esp_http_client_config_t config = {
        .url = url,
        .method = HTTP_METHOD_POST,

        .event_handler = _handle_cb,
    };
    client = esp_http_client_init(&config);
    // 设置请求头
    // char post_data[] = "{\"field1\":\"value1\",\"field2\":123}";
    esp_http_client_set_header(client, "Content-Type", "application/json");
    esp_http_client_set_post_field(client, post_data, strlen(post_data));

    // 执行请求
    esp_err_t err = esp_http_client_perform(client);
    if (err == ESP_OK)
    {
        ESP_LOGI(TAG, "HTTP Status = %d", esp_http_client_get_status_code(client));
    }
}
